from google.appengine.api import memcache
import hashlib
import random
import logging

def hash_url(url): # namespace now used.
    return hashlib.sha1(url).hexdigest()

def retrieveResponse(url,nameSpace):
    resp = memcache.get(hash_url(url), namespace=nameSpace)
    if resp and resp[4]:
        status_code = resp[0]
        headers = resp[1]
        content = resp[2]
        while resp[4]:
            resp = memcache.get(hash_url(str(resp[4])), namespace=nameSpace)
            content += resp[2]
        resp=(status_code,headers,content)
    return resp

def storePartResponse(url,response,nameSpace,time,request):
    storeResponse(url,response,nameSpace,time)
    _store("request_"+url,request,nameSpace,time)
    

def storeResponse(url,response,nameSpace,time):
    resp = response
    contentLength = len(resp.content)
    headerLength = len(str(resp.headers))#just roughly
    if contentLength+headerLength > 0xF0000:#we should split
        #first part
        end = 0xF0000-headerLength
        nextUrl = str(random.randint(0,0x1000000))
        saveContent = (resp.status_code,resp.headers,resp.content[0:end],contentLength,nextUrl)
        logging.info("fetch.saveToCache memche.add")
        _store(url,saveContent,nameSpace,time)
        logging.info("fetch.saveToCache finshed")
        while True:
            start = end
            end += 0xF0000
            currentUrl = nextUrl
            nextUrl = str(random.randint(0,0x1000000))
            if end > contentLength:
                end = contentLength
                nextUrl = None
            saveContent = (None,None,resp.content[start:end],contentLength,nextUrl)
            _store(currentUrl,saveContent,nameSpace,time)
            logging.debug("fetch.saveToCache loop")
            if end == contentLength:
                logging.debug("fetch.saveToCache done")
                break
    else:
        saveContent = (resp.status_code,resp.headers,resp.content,contentLength,None)
        logging.info("fetch.saveToCache " + str(time))
        _store(nextUrl,saveContent,nameSpace,time)

def _store(key,data,namespace,time=36000):
    h_key=hash_url(key)
    memcache.add(h_key,
                 data,
                 time=time,
                 namespace=namespace)

def _get(key,namespace):
    return memcache.get(hash_url(key), namespace=namespace)


    
